import { ResPage, Goods } from "@/api/interface/index";
import { Merchant } from "@/api/config/servicePort";
import http from "@/api";

/**
 * @name 商品管理模块
 */
// 获取商品列表
export const getAvailableGoodsList = (params: Goods.ReqGoodsParams) => {
  return http.post<ResPage<Goods.ResGoodsList>>(Merchant + `goods/list`, params);
};

/**
 * @name 商品管理模块
 */
// 获取商品列表
export const getPublishedGoodsList = (params: Goods.ReqGoodsParams) => {
  return http.post<ResPage<Goods.ResGoodsList>>(Merchant + `goods/published`, params);
};

// 获取树形商品列表
export const getUserTreeList = (params: Goods.ReqGoodsParams) => {
  return http.post<ResPage<Goods.ResGoodsList>>(Merchant + `user/tree`, params);
};

// 新增商品
export const addUser = (params: { id: string }) => {
  return http.post(Merchant + `/user/add`, params);
};

// 批量添加商品
export const BatchAddUser = (params: FormData) => {
  return http.post(Merchant + `/user/import`, params);
};

// 编辑商品
export const editProduct = (params: { id: string }) => {
  return http.post(Merchant + `goods/edit`, params);
};

// 删除商品
export const deleteUser = (params: { id: string[] }) => {
  return http.post(Merchant + `/user/delete`, params);
};

// 切换商品状态
export const changeUserStatus = (params: { id: string; status: number }) => {
  return http.post(Merchant + `/user/change`, params);
};

// 重置商品密码
export const publishGoods = (params: { item_id: string }) => {
  return http.post(Merchant + `goods/publish`, params);
};

export const removeGoods = (params: { item_id: string }) => {
  return http.post(Merchant + `goods/remove`, params);
};

export const recoverGoods = (params: { item_id: string }) => {
  return http.post(Merchant + `goods/recover`, params);
};

// 导出商品数据
export const exportUserInfo = (params: Goods.ReqGoodsParams) => {
  return http.download(Merchant + `/user/export`, params);
};

// 获取商品状态字典
export const getGoodsStatus = () => {
  return http.get<Goods.ResStatus[]>(Merchant + `dict/status`);
};

export const getTbkDetail = (params: { item_id: string }) => {
  http.get(Merchant + "goods/detail", params).then(res => {
    console.log(res);
    if ((res.data as { click_url: string }).click_url) {
      window.open((res.data as { click_url: string }).click_url);
    }
  });
};
